/**
 * 
 */
package com.java.controller.admin;

import java.util.List;

import javax.print.DocFlavor.STRING;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Pattern.Flag;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.java.entity.Good;
import com.java.entity.Image;
import com.java.entity.User;
import com.java.mapper.UserMapper;
import com.java.service.UserService;
import com.java.service.impl.GoodServiceImpl;
import com.java.service.impl.ImageServiceImpl;
import com.java.service.impl.UserServiceImpl;
import com.java.utils.md5Utils;

/**
* @author Kidney
* 创建时间：2019年1月22日 上午7:08:18
* Description:  http://localhost:8080/huahua//user/selectAll.shtml
*/

@Controller
@RequestMapping("/admin/")
public class AdminController {

	@Autowired
	private UserServiceImpl us;
	
	@Autowired
	private GoodServiceImpl gsl;
	
	

	@Autowired
	private ImageServiceImpl isl;

	/**
	 * 用户主页
	 */
	@RequestMapping("home")
	public ModelAndView userIndex() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/pages/front/userHome.jsp");
		return mav;
	}

	@RequestMapping("selectByPrimaryKey")
	@ResponseBody // 用来返回特定的值
	public User selectByPrimaryKey(Integer id) {
		return us.selectByPrimaryKey(id);

	}

	@RequestMapping("selectAll")
	@ResponseBody // 用来返回特定的值
	public List<User> selectAll() {
		return us.selectAll();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.java.mapper.UserMapper#deleteByPrimaryKey(java.lang.Integer)
	 */
	@ResponseBody // 用来返回特定的值
	@RequestMapping("deleteByPrimaryKey")
	public ModelAndView deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		
		ModelAndView mav = new ModelAndView();
		
		
		int flag =us.deleteByPrimaryKey(id);
		
		if (flag==0){
			mav.getModel().put("status", "删除错误");
		}else{
			mav.getModel().put("status", "删除用户"+id+"成功");
		}
		mav.setViewName("/pages/admin/user_manage.jsp");
		return mav ;
	}


	@RequestMapping("adminUpdate")
	public ModelAndView adminUpdateUser(int id) {
		ModelAndView mav = new ModelAndView();
		mav.getModel().put("id", id);
		mav.setViewName("/pages/admin/user_change_info.jsp");
		return mav;
	}
	
	
	/**
	 * @param id
	 * @param password
	 * @param address
	 * @param tele
	 * @param deposit
	 * @return
	 */
	@RequestMapping("adminBeginUpdate")
	public ModelAndView adminbeginUpdate(int id, String password,String address,String tele) {
		ModelAndView mav = new ModelAndView();
		
		User record = new User();
		
		record.setAddress(address);

		record.setPassword(md5Utils.md5(password));
		record.setTele(tele);
		record.setUserId(id);
		
		int flag = us.updateByPrimaryKeySelective(record);
		
		if(flag==1){
			
			mav.getModel().put("status", "修改用户"+id+"成功");
			
		}else{
			mav.getModel().put("status", "修改用户"+id+"失败");	
		}
		
		mav.setViewName("/pages/admin/user_manage.jsp");
		return mav;
	}
	
	
	
	
	
	
	
	@ResponseBody // 用来返回特定的值
	@RequestMapping("insertSelective")
	public int insertSelective(String userName, String password, String address, String tele) {

		User record = new User();
		record.setAddress(address);
		record.setPassword(password);
		record.setTele(tele);
		record.setUserName(userName);

		return us.insertSelective(record);
	}


	@ResponseBody // 用来返回特定的值
	@RequestMapping("updateByPrimaryKeySelective")
	public int updateByPrimaryKeySelective(String userName, String password, String address, String tele) {
		User record = new User();
		record.setAddress(address);
		record.setPassword(password);
		record.setTele(tele);
		record.setUserName(userName);
		return us.updateByPrimaryKeySelective(record);
	}

	@ResponseBody // 用来返回特定的值
	@RequestMapping("selectByUser")
	public User selectByUser(String userName, String password) {
		User record = new User();
		record.setPassword(password);
		record.setUserName(userName);
		return us.selectByUser(record);
	}

	@ResponseBody // 用来返回特定的值
	@RequestMapping("selectByName")
	public User selectByName(String userName) {
		// TODO Auto-generated method stub
		return us.selectByName(userName);
	}
	
	
	
	
	@RequestMapping("admin_good")
	public ModelAndView getAll(@RequestParam(defaultValue = "1") int pageNum) {
		int pageSize = 10;
		ModelAndView mav = new ModelAndView();
		List<Good> list = gsl.getPaged(pageSize, pageNum);
		mav.getModel().put("list", list);
		int count = gsl.count();
		// 计算一共有多少页
		int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
		mav.getModel().put("pageCount", pageCount);
		mav.getModel().put("pageNum", pageNum);
		mav.setViewName("/pages/admin/good_update_delete_insert.jsp");
		return mav;
	}
	
	
	@RequestMapping("getGoodDetails")
	public ModelAndView getGoodDetails(HttpSession session, int id) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/pages/admin/good_details.jsp");
		Good good = new Good();
		good = gsl.selectByPrimaryKey(id);
		List<Image> images = isl.selectByGoodId(id);
		mav.getModel().put("good", good);
		mav.getModel().put("images", images);
		return mav;
	}
	
	@RequestMapping("updateGood")
	public ModelAndView updateGood(int goodId) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/pages/admin/good_mod.jsp");
		mav.getModel().put("goodId", goodId);
		return mav;
	}
	
	
	
	

}
